package com.up4years.dataapi.controller;

import java.util.Date;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.up4years.controller.base.BaseController;
import com.up4years.dataapi.entity.SMSTextMessage;
import com.up4years.dataapi.service.support.SMSService;
import com.up4years.utils.ParamUtils;
import com.up4years.utils.ValidUtil;

@Controller
@RequestMapping("/sms")
public class SMSController extends BaseController {
	@Autowired
	private SMSService sMSService;
	
	/**
	 * 向指定手机发送验证码
	 * @param mobile
	 * @return
	 */
	@RequestMapping("/sendRegisterCode.htm")
	public ModelAndView sendRegisterCode(String mobile){
		
		if(StringUtils.isBlank(mobile)){
 			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("手机号不能为空", BaseController.RESCODE_FAIL));
		}
		if(!ValidUtil.validPhone(mobile)){
 			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("手机号格式错误", BaseController.RESCODE_FAIL));
		}
		if(!this.sMSService.sendAllowed(mobile)){
 			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("发送时间间隔不得少于一分钟", BaseController.RESCODE_FAIL));
		}
		SMSTextMessage textMessage =null;
		try {
			
			textMessage = this.sMSService.sendIdentifyMessage(mobile);
		} catch (Exception e) {
 			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("发送验证码失败", BaseController.RESCODE_FAIL));
		} 
		if(null!=textMessage&& 200==textMessage.getReturn_code() ){	
			JSONObject json = new JSONObject();
			json.put("phone", textMessage.getMobile());
			json.put("code", textMessage.getCode());
			json.put("messageId", textMessage.getId());
			return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult(json));
		}else{
 			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("发送验证码失败", BaseController.RESCODE_FAIL));
		}
	}
	/**
	 * 校验验证码
	 * @param mobile
	 * @param code
	 * @param messageId
	 * @return
	 */
	@RequestMapping("/checkRegisterCode.htm")
	public ModelAndView checkRegisterCode(String mobile,String code,String messageId){
		long id = ParamUtils.parm2Long(messageId, 0);
		if(id==0||StringUtils.isBlank(mobile)||StringUtils.isBlank(code)||StringUtils.isBlank(messageId)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("参数不合法", BaseController.RESCODE_FAIL));
		}
		SMSTextMessage textMessage = null;
		if(id!=0){			
			textMessage =this.sMSService.getById(id);
		}
 		if(textMessage==null||!textMessage.getCode().equals(code)||!textMessage.getMobile().equals(mobile)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("验证码错误", BaseController.RESCODE_FAIL));
		}
		if(textMessage!=null&& textMessage.getExpire_time().before(new Date())){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("验证码已过期", BaseController.RESCODE_FAIL));
		}
 		return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult());
	}
 
 }
